package edu.scsu.csci.tags;

/**
 * 
 */
import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import edu.scsu.csci.IExecutionError;
import edu.scsu.csci.core.ExecutionErrors;


/**
 * Shows the error messages that has been generated as the result of a conversion or validation error.
 * 
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
public class ExecutionErrorsTag extends TagSupport {
	
	/**
	 * serial version UID.
	 */
	private static final long serialVersionUID = 13423534L;

	public ExecutionErrorsTag() {
		super();
	}
	
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
	 */
	public int doStartTag() throws JspException {
		try {
			ExecutionErrors errors = (ExecutionErrors) pageContext.getRequest().getAttribute("errors");
			if(errors != null && errors.getList() != null && !errors.getList().isEmpty()){
				List<IExecutionError> errorsList = errors.getList();
				StringBuffer errorDesc = new StringBuffer(errorsList.size() * 100);
				errorDesc.append("<TABLE border='1' bgColor='#FF9999'><TBODY>");
				errorDesc.append("<TR><TD nowrap align='left' colspan='2'>")
						.append("<B>Errors: </B>")
						.append("Please correct the errors below and sumit the form again.</TD></TR>");
				for(IExecutionError error: errorsList){
					errorDesc.append("<tr>")
					.append("<td>")
					.append(error.getFieldName().toUpperCase())
					.append("</td>")
					.append("<td>")
					.append(error.getErrorMessage())
					.append("</td>")
					.append("</tr>");
				}
				errorDesc.append("</TBODY></table>");
				pageContext.getOut().print(errorDesc.toString());
			}
		} catch (IOException ioe) {
			throw new JspException("Error: IOException while writing to client"  + ioe.getMessage());
		}
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
}